<!--
 * @Author: C.
 * @Date: 2021-12-27 11:04:12
 * @LastEditTime: 2021-12-27 16:33:23
 * @Description: file content
-->
<template>
  <view class="j-form-style">
    <u--form
      labelPosition="top"
      errorType="toast"
      :model="formObj.form"
      ref="jForm"
      :rules="formObj.rules"
      :labelStyle="labelStyle"
    >
      <u-form-item
        :label="formItem.label"
        :prop="formItem.prop"
        v-for="formItem in formObj.formSchema"
        :key="formItem.prop"
      >
        <FormItem
          :cdata="formItem"
          v-model="formObj.form[formItem.prop]"
        ></FormItem>
      </u-form-item>
    </u--form>
    <u-button @click="validate">jy</u-button>
  </view>
</template>

<script>
import FormItem from "./cpns/FormItem.vue";
import _Form from "@/jv_doc/class/Form/_Form";
import { $j_fz, $j_title, $j_content } from "../../styles/theme";
export default {
  props: {
    register: {
      type: Object,
      default: () => {}
    }
  },
  components: {
    FormItem
  },
  data() {
    return {
      formObj: {},
      labelStyle: {
        fontWeight: 600,
        color: "#969696"
      }
    };
  },
  created() {
    this.formObj = new _Form({
      formSchema: this.register.formSchema || [],
      form: this.register.form
    });
  },
  methods: {
    validate(fn) {
      this.$refs.jForm
        .validate()
        .then((res) => {
          fn(true);
        })
        .catch((err) => {
          let errMsg = err[0]?.message || "校验失败";
          uni.$u.toast(errMsg);
          fn(false);
        });
    },
    getForm() {
      return this.formObj.form;
    },
    reset() {
      this.formRef.resetFields();
    }
  },
  onReady() {
    // this.formObj.formRef = this.$refs.jForm;
  }
};
</script>

<style lang="scss">
.j-form-style {
  padding: 20rpx;
}
</style>
